package GUILayer;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.ArrayList;

import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JComboBox;
import javax.swing.JTextField;
import javax.swing.JTable;
import javax.swing.JScrollPane;

import ControlLayer.BookingCtr;
import ExceptionLayer.CustomException;
import ModelLayer.Booking;

import javax.swing.JButton;
import javax.swing.table.DefaultTableModel;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class ManageBookings extends JPanel {
	private JTextField txtFromDate;
	private JTable tblResult;
	private JTextField txtToDate;
	private JLabel lblFra;
	private DefaultTableModel tblModel;

	/**
	 * Create the panel.
	 */
	public ManageBookings() {
		setLayout(null);
		
		JLabel lblFind = new JLabel("Find booking p\u00E5 dato:");
		lblFind.setBounds(32, 41, 127, 14);
		add(lblFind);
		
		txtFromDate = new JTextField();
		txtFromDate.setToolTipText("YYYY-MM-dd");
		txtFromDate.setText("2013-01-01");
		txtFromDate.setBounds(220, 38, 103, 20);
		add(txtFromDate);
		txtFromDate.setColumns(10);
		
		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setBounds(32, 95, 1007, 174);
		add(scrollPane);
		
		tblResult = new JTable();
		scrollPane.setViewportView(tblResult);
		
		JLabel lblTil = new JLabel("Til:");
		lblTil.setBounds(333, 41, 46, 14);
		add(lblTil);
		
		txtToDate = new JTextField();
		txtToDate.setToolTipText("YYYY-MM-dd");
		txtToDate.setText("2014-01-01");
		txtToDate.setBounds(389, 38, 103, 20);
		add(txtToDate);
		txtToDate.setColumns(10);
		
		lblFra = new JLabel("Fra:");
		lblFra.setBounds(164, 41, 46, 14);
		add(lblFra);
		
		JButton btnSearch = new JButton("S\u00F8g");
		btnSearch.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) 
			{
				findByDateToDate();
			}
		});
		btnSearch.setBounds(529, 37, 89, 23);
		add(btnSearch);
		
		JButton btnNewButton = new JButton("Slet booking");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) 
			{
				try {
					deleteBooking();
				} catch (CustomException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		});
		btnNewButton.setBounds(32, 290, 127, 23);
		add(btnNewButton);
		
		JButton btnSale = new JButton("Overf\u00F8r til salg");
		btnSale.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) 
			{
				transferToSale();
			}
		});
		btnSale.setBounds(196, 290, 183, 23);
		add(btnSale);
		
		
		

	}
	
	private void createTable(ArrayList<Booking> result)
	{
		tblModel = new DefaultTableModel();
		
		tblModel.addColumn("ID:");
		tblModel.addColumn("Treatment:");
		
		tblModel.addColumn("Employee:");
		tblModel.addColumn("Customer:");
		tblModel.addColumn("Date");
		tblModel.addColumn("StartTime:");
		tblModel.addColumn("EndTime:");
		tblModel.addColumn("Status");
		tblModel.addColumn("DateCreated:");
		
		for(Booking book : result)
		{
			Object[] row = {book.getId(), book.getTreatment().getTitle(), (book.getHairDresser().getFirstName() + " " + book.getHairDresser().getLastName()), 
					(book.getCustomer().getFirstName() + " " + book.getCustomer().getLastName()), 
					book.getDate(), book.getStartTime(), book.getEndTime(), book.getStatus(), book.getDateCreated()};
			
			tblModel.addRow(row);
		}
		
		tblResult.setModel(tblModel);
		tblResult.getColumnModel().getColumn(1).setPreferredWidth(120);
	}
	
	private void findByDateToDate()
	{
		BookingCtr bookCtr = new BookingCtr();
		String fromDate = txtFromDate.getText();
		String toDate = txtToDate.getText();
		ArrayList<Booking> bookings = bookCtr.getBookingsByDateToDate(fromDate, toDate);
		createTable(bookings);
	}
	

	
	private void deleteBooking() throws CustomException
	{
		
		int row = tblResult.getSelectedRow();
		int id = (int)tblModel.getValueAt(row, 0);
		BookingCtr bookCtr = new BookingCtr();
		bookCtr.deleteBookingById(id);
		tblModel.removeRow(tblResult.getSelectedRow());
		tblResult.setModel(tblModel);
	}
	
	private void transferToSale()
	{
		int row = tblResult.getSelectedRow();
		int id = (int)tblModel.getValueAt(row, 0);
		BookingCtr bookCtr = new BookingCtr();
		bookCtr.getBookingById(id);
	}
}
























